var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');

var app = getApp();

Page({
  data: {
    user: null,
    qualificationsList: [],
    code: '',
    status: 0,
    page: 1,
    limit: 10,
    count: 0,
    scrollTop: 0,
    showPage: false
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if(options.status == 1){
      this.setData({
        status:1
      });
    }
    this.goQualificationsLoad();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    // this.getQualificationsList();
    this.goQualificationsLoad();
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  goQualificationsLoad:function(){
    if(this.data.status == 1){
      this.getQualificationsList();
    }else{
      this.getQualificationsInfo();
    }
  },
  getQualificationsInfo: function() {
    let that = this;
    util.request(api.QualificationsMyInfo).then(function(res) {
      if (res.errno === 0) {
        that.setData({
          user:res.data.userVo||null
        });
      }
    });
  },
  getQualificationsList: function() {

    let that = this;
    that.setData({
      scrollTop: 0,
      showPage: false,
      qualificationsList: []
    });
    util.request(api.QualificationsMyList, {
      status: that.data.status,
      page: that.data.page,
      limit: that.data.limit
    }).then(function(res) {
      if (res.errno === 0) {
        that.setData({
          scrollTop: 0,
          qualificationsList: res.data.list,
          showPage: res.data.total > that.data.limit,
          count: res.data.total
        });
      }
    });

  },
  goResetAuth: function() {
    if (app.globalData.hasLogin) {
      util.request(api.CouuntTargetStatus, {
        status: 0
      }).then(function(res) {
        if (res.errno === 0) {
          if(res.data > 0){
            wx.showModal({
              title: '错误信息',
              content: '存在未审核的认证信息',
              showCancel: false
            });
          }else{
            wx.navigateTo({
              url: "/pages/auth/authenticationIdentity/authenticationIdentity"
            });
          }
        }
      });
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    };
  },
  nextPage: function(event) {
    var that = this;
    if (this.data.page > that.data.count / that.data.limit) {
      return true;
    }

    that.setData({
      page: that.data.page + 1
    });

    this.getQualificationsList();

  },
  prevPage: function(event) {
    if (this.data.page <= 1) {
      return false;
    }

    var that = this;
    that.setData({
      page: that.data.page - 1
    });
    this.getQualificationsList();
  },
  switchTab: function(e) {

    this.setData({
      qualificationsList: [],
      status: e.currentTarget.dataset.index,
      page: 1,
      limit: 10,
      count: 0,
      scrollTop: 0,
      showPage: false
    });
    if(this.status == 0){
      this.getQualificationsInfo();
    }else{
      this.getQualificationsList();
    }
  },
  openQualificationsInfo(event) {
    let that = this;
    let index = event.currentTarget.dataset.index;
    let authId = that.data.user.authId || 0;
    var prefix = '/pages/auth/authenticationIdentityInfo/authenticationIdentityInfo?isAuth=1&id='
    wx.navigateTo({
      url: prefix+authId,
    });
  },
  openApprovalQualificationsInfo(event){
    let that = this;
    let index = event.currentTarget.dataset.index;
    let valueId = this.data.qualificationsList[index].id;
    
    var prefix = '/pages/auth/authenticationIdentityInfo/authenticationIdentityInfo?isAuth=0&id='
    wx.navigateTo({
      url: prefix+valueId,
    });
  },
})